import React, { Component } from "react";
import axios from "axios";

export default class Notify extends Component {
	constructor(props) {
		super(props);
		this.state = {
			newVersion: process.env.version,
			version: process.env.version
		};
	}

	componentDidMount() {
		const versions =
			"https://www.fastmock.site/mock/1529fa78fa4c4880ad153d115084a940/yapi/versions";
		axios.get(versions).then(req => {
			if (req.status === 200) {
				this.setState({ newVersion: req.data.data[0] });
			} else {
				message.error("无法获取新版本信息！");
			}
		});
	}

	render() {
		const isShow = this.state.newVersion !== this.state.version;
		return (
			<div>
				{isShow && (
					<Alert
						message={
							<div>
								当前版本是：{this.state.version}&nbsp;&nbsp;可升级到:{" "}
								{this.state.newVersion}
								&nbsp;&nbsp;&nbsp;
								<a
									target="view_window"
									href="https://github.com/YMFE/yapi/blob/master/CHANGELOG.md">
									版本详情
								</a>
							</div>
						}
						banner
						closable
						type="info"
					/>
				)}
			</div>
		);
	}
}
